/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.util;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

public class FileBrowser {
    List mDirList;
    List mFileList;
    
    String mCurrentPath;
    String mRootDirectory = "D:";
    String mStartDir = "D:";

    public FileBrowser(String pRootDirectory) {
        mRootDirectory = pRootDirectory;
    }

    public FileBrowser(String pApplTop, String pRoot) {
        mRootDirectory = pApplTop + pRoot;
        mStartDir = pRoot;
    }

    public List getDirList() {
        return mDirList;
    }

    public List getFileList() {
        return mFileList;
    }

    public void stepIn(String fileName, String strPath) {
        stepIn(fileName, strPath, null);
    }

    public void stepIn(String fileName, String strPath, String ext) {
        if (strPath == null) {
            strPath = "";
        }

        File path = new File(mRootDirectory + File.separator + strPath);
        String[] list;
        mDirList = new ArrayList();
        mFileList = new ArrayList();

        list = path.list();

        if (fileName != null) {
            File newFile = new File(path.getPath() + File.separator + fileName);

            if (newFile.isDirectory()) {
                strPath = strPath + File.separator + fileName;
                path = new File(mRootDirectory + File.separator + strPath);
                list = path.list();
            }
        } else {
            fileName = "not specified";
        }

        if (list == null) {
            return;
        }

        for (int i = 0; i < list.length; i++) {
            File tempFile = new File(mRootDirectory + strPath + File.separator + list[i]);

            if (tempFile.isDirectory()) {
                mDirList.add(list[i]);
            } else {
                if ((ext == null) || tempFile.getName().endsWith(ext)) {
                    mFileList.add(tempFile);
                }
            }
        }
    }
}
